package com.lagou.dao.impl;

import com.lagou.dao.CourseLessonDao;
import com.lagou.pojo.Course_Lesson;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

public class CourseLessonDaoImpl implements CourseLessonDao {

    // 保存课时信息
    @Override
    public int saveCourseLesson(Course_Lesson lesson) {
        try{
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO course_lesson(\n" +
                    "course_id,\n" +
                    "section_id,\n" +
                    "theme,\n" +
                    "duration,\n" +
                    "is_free,\n" +
                    "order_num,\n" +
                    "`status`,\n" +
                    "create_time,\n" +
                    "update_time) VALUES(?,?,?,?,?,?,?,?,?);";
            Object[] param = {lesson.getCourse_id(), lesson.getSection_id(), lesson.getTheme(),
            lesson.getDuration(), lesson.getIs_free(), lesson.getOrderNum(), lesson.getStatus(),
            lesson.getCreate_time(), lesson.getUpdate_time()};
            int row = qr.update(sql, param);
            return row;
        }
        catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

    @Override
    public int updateCourseLesson(Course_Lesson course_lesson) {
        try{
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course_lesson\n" +
                    "set course_id = ?,\n" +
                    "section_id = ?,\n" +
                    "theme = ?,\n" +
                    "duration = ?,\n" +
                    "is_free = ?,\n" +
                    "order_num = ?,\n" +
                    "update_time = ?\n" +
                    "where id = ?";
            Object[] param = {course_lesson.getCourse_id(), course_lesson.getSection_id(), course_lesson.getTheme(),
            course_lesson.getDuration(), course_lesson.getIs_free(), course_lesson.getOrderNum(), course_lesson.getUpdate_time(),
            course_lesson.getId()};
            int row = qr.update(sql, param);
            return row;
        }
        catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
